<?php
/**
 * 招标控制器
 */
namespace Admin\Controller;
use Common\Controller\WahahabaseController;
class TenderController extends WahahabaseController{
	protected $users_model;
	public function _initialize() {
		parent::_initialize();
		$this->users_model = D("Common/Users");
		$this->deal_model = D("Common/Tender");
		#上传附件
		$this->att_model = D("Common/Attachment");
	}
	/**
	 * 查询列表数据
	 **/
	public function indexPage($where){
		$eq = array(
				"eq" => "=",
				"neq" => "!=",
				"gt" => ">",
				"egt" => ">=",
				"lt" => "<",
				"elt" => "<=",
				"like" => "LIKE"
		);
		// 当前登录人
		$adminId = session('ADMIN_ID');
		$condition = "(
            (d.is_publish = 0 AND (d.add_user = $adminId OR ((SELECT department_id FROM hzzy_users WHERE id=$adminId) = d.add_department) AND ((SELECT role_id FROM hzzy_users WHERE id=$adminId) IN('1','2','3'))))
            OR
            (d.is_publish = 1)
        )";
		foreach($where as $key => $v){
			if(count($v) > 1){
				$d = $eq[$v[0]] == NULL ? '=' : $eq[$v[0]];
				if($d != 'LIKE'){
					$condition .= " AND $key $d '".$v[1]."'";
				} else {
					$condition .= " AND $key $d '%".$v[1]."%'";
				}
			} else {
				$condition .= " AND $key = '$v'";
			}
		}
		$count=$this->deal_model->alias("d")->where($condition)->count();
		$page = $this->page($count, 20);

		$results = $this->deal_model->alias("d")
				->field('d.*,u.real_name')
				->join(array(
						' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
				))
				->where($condition)
				->order("d.add_time DESC")
				->limit($page->firstRow, $page->listRows)
				->select();
		//exit($this->deal_model->getLastSql());
		$this->assign("page", $page->show('Admin'));
		$this->assign("results",$results);
		$id = I('get.id',0,'intval');
		//$re = $this->att_model->where(array("table_id"=>$id, "belong_to"=>1))->select();
		$re=$this->att_model->alias("a")
				 //->join("tender on tender.id=attachment.table_id")
				 ->join(array(
						' LEFT JOIN '.C('DB_PREFIX').'tender t ON a.table_id = t.id',
				))
				->where("belong_to= 12")
				 ->select();
		//var_dump($re);
		$this->assign("re", $re);
		//exit($this->att_model->getLastSql());
		$this->display();
	}
	/**
	 * 主页
	 */
	public function index(){
			$where=array("d.is_deleted"=>array('neq',1));
			$this->getDepartLevel();
			$this->indexPage($where);
			
		}
		
	/**
     *  添加
     */
    public function add(){
    	// 查询当前登录用户真是姓名	
    	$username = $this->users_model
    				     ->where(array("id" => session('ADMIN_ID')))
    				     ->getField("real_name",true);			     
    	$this->assign("us_name",$username[0]);
		$this->assign("add_time", date('Y-m-d H:i:s'));
		$this->display( );
		}
	
	public function add_post(){
		//var_dump($_POST);
		if(IS_POST){
			if(!I("post.fileName")){
				$this->error("请上传需要上传的文件！");
			}
			//合并底层数据
			$data =$this->saveData();
			$data['file_type'] = $_POST["file_type"];
			$data['file_name'] = $_POST['fileName'][0];
			//var_dump($data);
			if ($this->deal_model->create()!==false) {
				$result = $this->deal_model->add($data);
				if ($result!==false) {
					$this->saveFile($result, 12);
					$this->success("添加成功！", U("Tender/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	

	public function view(){
			$id = I('get.id',0,'intval');
			$result = $this->deal_model->alias("d")
			->field('d.*,u.real_name')
			->join(array(
				' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
			))->where(array("d.id"=>$id))->find();
			$results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>12))->select();
			
			$this->assign("result", $result);
			$this->assign("results", $results);
			
			$this->display();
		}

	public function edit(){
		$id = I('get.id',0,'intval');
		$result = $this->deal_model->alias("d")
			->field('d.*,u.real_name')
			->join(array(
				' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
			))->where(array("d.id"=>$id))->find();
		$results = $this->att_model->where(array("table_id"=>$id, "belong_to"=>12))->select();
		
		$this->assign("result", $result);
		$this->assign("results", $results);
		$this->display();
	}
	public function edit_post(){
		if (IS_POST) {
			$data = array_merge($_POST, $this->saveData(2));
			if ($this->deal_model->create()!==false) {
				$result=$this->deal_model->save($data);
				if ($result !== false) {
					$this->saveFile(I("post.id"), 12, 2);
					$this->success("保存成功！", U("Tender/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->sPlan_model->getError());
				}
			}
	}	

}
?>